package xyz.app.domain.addresses;

import lombok.*;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import xyz.app.DaoRegistry;
import xyz.app.Dao;
import xyz.app.dependencies.HasOwners;
import xyz.app.dependencies.OwnerEntry;
import xyz.app.domain.parties.AbstractParty;
import xyz.app.domain.parties.AbstractParty_;

import java.util.Collection;
import java.util.HashSet;

@Slf4j
@Service
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class DaoAddress implements Dao<Address>,
        HasOwners<Address> {

    @Getter @Accessors(fluent = true)
    private final DaoRegistry daoRegistry;

    private final Collection<OwnerEntry<?, Address>> ownersMapping = new HashSet<>();

    @Override
    public Collection<OwnerEntry<?, Address>> ownersMapping() {
        if (ownersMapping.isEmpty()) {
            daoRegistry.ownerEntry(AbstractParty_.address, AbstractParty.class).ifPresent(ownersMapping::add);
            daoRegistry.ownerEntry(LegalAddress_.address, LegalAddress.class).ifPresent(ownersMapping::add);
        }

        return ownersMapping;
    }

    @Override
    public @NonNull Class<Address> domainClass() {
        return Address.class;
    }
}